from rest_framework.exceptions import ValidationError

from apps.master.models import SystemParametersModel
from apps.project.models import ProjectProcessModel
from apps.projectbases.models import ProjectPlanHModel
from utils.utils import update_set
from django.utils import translation
from apps.codetable.handles import field_codetable


class ProjectProcessService:
    def __init__(self, request) -> None:
        self.request = request
        super().__init__()
        language = translation.get_language()
        self.status_dict = field_codetable('project_status', localization=language, model='Project')



    def createProjectProcess(self, project_status:str,  request):
        try:
            projectPlanFK = request.projectPlanFK
            project_name = request.project_name
            project_id = request.id

            # 获取项目当前状态对应的中文名称
            project_status_desc = self.status_dict.get(project_status) if self.status_dict else project_status
            description = "【" + project_status_desc + "】" + project_name

            # 获取参数表中设定的进度报告样式
            systemQuery = SystemParametersModel.objects.get(param_code='REPORTFORM')
            report_form = systemQuery.param_value

            # 获取当前登录用户
            user = request.created
            created_date = request.created_date
            created_program = request.created_program
            updated = request.updated
            updated_date = request.updated_date
            updated_program = request.updated_program

            data = {
                'projectPlanFK': projectPlanFK,
                'projectId': project_id,
                'recorder': user,
                'report_recipient': None,
                'report_form': report_form,
                'note': description,
                'created': user,
                'created_date': created_date,
                'created_program': created_program,
                'updated': updated,
                'updated_date': updated_date,
                'updated_program': updated_program
            }
            result = ProjectProcessModel.objects.create(**data)

            return result
        except Exception as e:
            raise ValidationError(e)

